<template>
  <div class="title">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/admin/employee' }">员工管理</el-breadcrumb-item>
      <el-breadcrumb-item>编辑员工</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <div class="add_employee">
    <div class="container">
      <div class="fill_info">
        <el-form
            ref="ruleFormRef"
            style="max-width: 600px"
            :model="ruleForm"
            :rules="rules"
            label-width="auto"
            class="demo-ruleForm"
            :size="formSize"
            status-icon
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="ruleForm.username" placeholder="请输入账号"/>
          </el-form-item>
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入员工姓名"/>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="ruleForm.phone" placeholder="手机号"/>
          </el-form-item>
          性别：
          <el-radio-group v-model="ruleForm.sex">
            <el-radio :value="1" size="large">男</el-radio>
            <el-radio :value="0" size="large">女</el-radio>
          </el-radio-group>
          <el-form-item label="身份证" prop="idNumber">
            <el-input v-model="ruleForm.idNumber" placeholder="身份证"/>
          </el-form-item>
        </el-form>
      </div>

      <div class="handleOperation">
        <el-button type="primary"  @click="submitForm(ruleFormRef)">保存</el-button>
        <el-button type="primary">返回</el-button>

      </div>

    </div>
  </div>


</template>

<script setup>
import {onMounted, reactive, ref} from 'vue';
import {getById, updateEmployee} from "@/api/admin-api.js";
import {ElMessage} from "element-plus";
import {useRoute, useRouter} from "vue-router";
const ruleForm = ref({
  username:"",
  name:"",
  phone:'',
  sex:0,
  idNumber:"",
})


const ruleFormRef = ref()
const rules = reactive({
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 3, max: 9, message: 'Length should be 3 to 9', trigger: 'blur' },
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' }
  ],
  idNumber: [
    { required: true, message: '请输入身份证号  ', trigger: 'blur' }
  ],

})


const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let res = null
      try {
        console.log(ruleForm.value);
        res = await updateEmployee(ruleForm.value);
        console.log(res);
        console.log('submit!')
        ElMessage.success("更新成功")
        await router.push('/admin/employee')
      }catch (e){
        ElMessage.error(res.message)
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

const route = useRoute()
const router = useRouter()

onMounted(async ()=>{
  console.log(route.params.id)
  const res = await getById(route.params.id)
  ruleForm.value = res.data
})
</script>
<style lang="scss" scoped>
.title{
  height: 50px;
  width: 100%;
  background-color: #fff;
}
.add_employee{
  margin-top: 10px;
  width:100%;
  height:400px;
  background-color: #f3f3f3;
  display:flex;
  justify-content: center;
  align-items: center;

  .container{
    width:97%;
    height:95%;
    background-color:white;
    .handleOperation{
      width: 100%;
      height: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>